﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class AllWarnInfo : BaseChildForm
    {
        private QunYing.BLL.DeviceRealTimeInfo realTimeInfo= new QunYing.BLL.DeviceRealTimeInfo();
        public AllWarnInfo()
        {
            InitializeComponent();
            BindData();
            this.sharpGrid.AllowPopupMenu = true;
            this.sharpGrid.AddPopupMenu(new ToolStripSeparator(), null);
            this.sharpGrid.AddPopupMenu(new ToolStripMenuItem("置为已处理"), this.WarnInfoProcessed);
            this.sharpGrid.AddPopupMenu(new ToolStripMenuItem("查看设备信息"),this.LookOverDeviceInfo);
            this.sharpGrid.AddPopupMenu(new ToolStripMenuItem("出警单"), ShowWarnOrder);
            GridView view = this.sharpGrid.MainView as GridView;
            
        }

        public override void BindData()
        { 
            DateTime dt = DateTime.Today.AddDays(-60);
            dt = BindData(dt);
        }

        private void WarnInfoProcessed(object sender, EventArgs e)
        {
            try
            {
                DataRow dr = this.GetWarnInfo();
                if (dr == null)
                {
                    GlobalInfo.MsgBoxShowError("请选择一行报警数据");
                    return;
                }

                DataTable dt = dr.Table.Clone();
                dt.ImportRow(dr);
                var warnInfo = realTimeInfo.DataTableToList(dt)[0];
                if (warnInfo.告警是否解除 != null)
                {
                    if (!warnInfo.告警是否解除.Value)
                    {
                        QunYing.DAL.DeviceRealTimeInfo dti = new QunYing.DAL.DeviceRealTimeInfo();
                        dti.UpdataProcessedSatu(warnInfo.ID.ToString());
                    }
                    else
                    {
                        GlobalInfo.MsgBoxShowInfo("报警已处理，不需重新处理！");
                    }
                }
                else
                {
                    QunYing.DAL.DeviceRealTimeInfo dti = new QunYing.DAL.DeviceRealTimeInfo();
                    dti.UpdataProcessedSatu(warnInfo.ID.ToString());
                }
                GlobalInfo.MsgBoxShowInfo("报警已处理！");
                BindData();
            }
            catch (Exception ex)
            {
                QunYing.Common.Loger.Log.Error("手动更改报警数据时：" + ex.Message);
            }
        }

   
        private void ShowWarnOrder(object sender, EventArgs e)
        {
            DataRow dr = this.GetWarnInfo();
            if (dr == null)
            {
                GlobalInfo.MsgBoxShowError("请选择一行报警数据");
                return;
            }
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                if (dr["告警处理单据ID"] is DBNull)
                {
                    FormWarnOrderAdd fwe = new FormWarnOrderAdd();
                    QunYing.BLL.DeviceRealTimeInfo dti = new QunYing.BLL.DeviceRealTimeInfo();
                    fwe.BindData(dti.GetModel((int)dr["ID"]));
                    if (fwe.ShowDialog() == DialogResult.OK)
                    {
                        this.BindData(DateTime.Today.AddDays(-60));
                    }
                }
                else
                {
                    var fwe = new FormWarnOrderEdit();
                    QunYing.BLL.DeviceRealTimeInfo dti = new QunYing.BLL.DeviceRealTimeInfo();
                    fwe.BindData(dti.GetModel((int)dr["ID"]));
                    if (fwe.ShowDialog() == DialogResult.OK)
                    {
                        this.BindData(DateTime.Today.AddDays(-60));
                    }
                }
            }
            else
            {
                var fwe = new FormWarnOrderEdit();
                QunYing.BLL.DeviceRealTimeInfo dti = new QunYing.BLL.DeviceRealTimeInfo();
                fwe.BindData(dti.GetModel((int)dr["ID"]));
                if (fwe.ShowDialog() == DialogResult.OK)
                {
                    this.BindData(DateTime.Today.AddDays(-60));
                }
            }
        }


        private DateTime BindData(DateTime dt)
        {
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                DataTable dataTable = realTimeInfo.GetAllWarnForCS(GlobalInfo.UserLoginInfo.用户ID,dt,true).Tables[0];
                this.sharpGrid.DataSource = dataTable;
            }
            if (GlobalInfo.UserLoginInfo.角色 == "平台管理员")
            {
                DataTable dataTable = realTimeInfo.GetAllWarnForCS(GlobalInfo.UserLoginInfo.用户ID, dt, false).Tables[0];
             
                this.sharpGrid.DataSource = dataTable;
            }
            GridView view = this.sharpGrid.MainView as GridView;
            
            view.Columns["数据发送时间"].DisplayFormat.FormatString = "t";
            view.Columns["数据接收时间"].DisplayFormat.FormatString = "t";
            view.Columns["告警解除时间"].DisplayFormat.FormatString = "t";
            view.Columns["ID"].Visible = false;
            return dt;
        }



        public void LookOverDeviceInfo(object sender, EventArgs e)
        {
            DataRow row = this.GetWarnInfo();
            if (row == null)
            {
                GlobalInfo.MsgBoxShowError("请选择一行报警数据");
                return;
            }
            QunYing.BLL.ICSA_Device bll_icsa = new QunYing.BLL.ICSA_Device();
            var v=  bll_icsa.GetModel(row["设备ID"].ToString());
            if (v != null)
            {
                List<QunYing.Model.ICSA_Device> model = new List<QunYing.Model.ICSA_Device>() { v };
                FormICSALook form = SingleObjManager.GetSingleObj<FormICSALook>();
                form.BindData(model);
                form.ShowDialog();
            }
          
        }


        private DataRow GetWarnInfo()
        {
            GridView view = this.sharpGrid.MainView as GridView;
            int[] rows= view.GetSelectedRows();
            if (rows.Length == 0)
                return null;
            foreach (var v in rows)
            {
                if (v < 0)
                    continue;
               DataRowView rowView= view.GetRow(v) as DataRowView;
               if (rowView == null)
                   return null;
               return rowView.Row;
            }
            return null;

        }

        public new void Show()
        {
            GlobalInfo.MainForm.ActivedChildForm(this);
        }

        private void btn_查询_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.comboBoxEdit1.Text))
                return;
            if (this.comboBoxEdit1.SelectedIndex == 0)
            {
                DateTime dt = DateTime.Today.AddDays(-7);
                dt = BindData(dt);
            }
            else if (this.comboBoxEdit1.SelectedIndex == 1)
            {
                DateTime dt = DateTime.Today.AddDays(-15);
                dt = BindData(dt);
            }
            else if (this.comboBoxEdit1.SelectedIndex == 2)
            {
                DateTime dt = DateTime.Today.AddMonths(-1);
                dt = BindData(dt);
            }
            else if (this.comboBoxEdit1.SelectedIndex == 3)
            {
                DateTime dt = DateTime.Today.AddMonths(-6);
                dt = BindData(dt);
            }
            else if (this.comboBoxEdit1.SelectedIndex == 4)
            {
                DateTime dt = DateTime.Today.AddYears(-1);
                dt = BindData(dt);
            }
            
        }

        private void sharpGrid_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            ShowWarnOrder(null, null);
        }
    }
}